﻿using System;
namespace Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="SooritaKanne_v4", Namespace="http://producers.arireg.xtee.riik.ee/producer/arireg", Target=typeof(SooritaKanneV4))]
    public class SooritaKanneV4Serializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected ISooritaKanneV4 clazz;
        
        public SooritaKanneV4Serializer(ISooritaKanneV4 clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("kande_paritolu");
            if (clazz.KandeParitolu!=null) {
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "string");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdString(((string)(clazz.KandeParitolu))).ToString());
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : KandeParitolu");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("kande_id");
            if (clazz.KandeId!=null) {
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "string");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdString(((string)(clazz.KandeId))).ToString());
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : KandeId");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("ekande_liik");
            Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "int");
            Writer.WriteString(new global::Xtee.Core.Schema.XsdInt(((int)(clazz.EkandeLiik))).ToString());
            Writer.WriteEndElement();
            if (clazz.OnParanduskanne!=null) {
                Writer.WriteStartElement("on_paranduskanne");
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "boolean");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdBoolean(((bool)(clazz.OnParanduskanne))).ToString());
                Writer.WriteEndElement();
            }
            if (clazz.EelmineLahendLegacy!=null) {
                Writer.WriteStartElement("eelmine_lahend_legacy");
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "string");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdString(((string)(clazz.EelmineLahendLegacy))).ToString());
                Writer.WriteEndElement();
            }
            if (clazz.EelminePaevikukandeId!=null) {
                Writer.WriteStartElement("eelmine_paevikukande_id");
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "string");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdString(((string)(clazz.EelminePaevikukandeId))).ToString());
                Writer.WriteEndElement();
            }
            if (clazz.OnKiirmenetlus!=null) {
                Writer.WriteStartElement("on_kiirmenetlus");
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "boolean");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdBoolean(((bool)(clazz.OnKiirmenetlus))).ToString());
                Writer.WriteEndElement();
            }
            Writer.WriteStartElement("kande_kuupaev");
            Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "date");
            Writer.WriteString(new global::Xtee.Core.Schema.XsdDate(((System.DateTime)(clazz.KandeKuupaev))).ToString());
            Writer.WriteEndElement();
            if (clazz.EsitajaNimi!=null) {
                Writer.WriteStartElement("esitaja_nimi");
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "string");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdString(((string)(clazz.EsitajaNimi))).ToString());
                Writer.WriteEndElement();
            }
            if (clazz.EsitajaKood!=null) {
                Writer.WriteStartElement("esitaja_kood");
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "string");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdString(((string)(clazz.EsitajaKood))).ToString());
                Writer.WriteEndElement();
            }
            if (clazz.Asutamisnumber!=null) {
                Writer.WriteStartElement("asutamisnumber");
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "string");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdString(((string)(clazz.Asutamisnumber))).ToString());
                Writer.WriteEndElement();
            }
            if (clazz.Ametitoiming!=null) {
                Writer.WriteStartElement("ametitoiming");
                global::Xtee.Core.SerializationMetadata metaAmetitoiming = this.GetMetaData(clazz.Ametitoiming);
                Writer.WriteTypeAttribute(metaAmetitoiming.TypeQualifiedName.Namespace, metaAmetitoiming.TypeQualifiedName.Name);
                metaAmetitoiming.NewSerializer(clazz.Ametitoiming).WriteXml(Writer);
                Writer.WriteEndElement();
            }
            for (int x = 0; ((clazz.Riigiloivud != null) 
                        && (x < clazz.Riigiloivud.Count)); x = (x + 1)) {
                if (clazz.Riigiloivud[x]!=null) {
                    Writer.WriteStartElement("riigiloivud");
                    global::Xtee.Core.SerializationMetadata metaRiigiloivud = this.GetMetaData(clazz.Riigiloivud[x]);
                    Writer.WriteTypeAttribute(metaRiigiloivud.TypeQualifiedName.Namespace, metaRiigiloivud.TypeQualifiedName.Name);
                    metaRiigiloivud.NewSerializer(clazz.Riigiloivud[x]).WriteXml(Writer);
                    Writer.WriteEndElement();
                }
            }
            if (clazz.Markus!=null) {
                Writer.WriteStartElement("markus");
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "string");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdString(((string)(clazz.Markus))).ToString());
                Writer.WriteEndElement();
            }
            if (clazz.RegistriPiirkond!=null) {
                Writer.WriteStartElement("registri_piirkond");
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "string");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdString(((string)(clazz.RegistriPiirkond))).ToString());
                Writer.WriteEndElement();
            }
            if (((clazz.Ettevotjad == null) 
                        || (clazz.Ettevotjad.Count < 1m))) {
                throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "ettevotjad");
            }
            for (int x = 0; ((clazz.Ettevotjad != null) 
                        && (x < clazz.Ettevotjad.Count)); x = (x + 1)) {
                Writer.WriteStartElement("ettevotjad");
                if (clazz.Ettevotjad[x]!=null) {
                    global::Xtee.Core.SerializationMetadata metaEttevotjad = this.GetMetaData(clazz.Ettevotjad[x]);
                    Writer.WriteTypeAttribute(metaEttevotjad.TypeQualifiedName.Namespace, metaEttevotjad.TypeQualifiedName.Name);
                    metaEttevotjad.NewSerializer(clazz.Ettevotjad[x]).WriteXml(Writer);
                }
                else {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Ettevotjad");
                }
                Writer.WriteEndElement();
            }
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="kande_paritolu") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKanne_v4>kande_paritolu");
                }
                clazz.KandeParitolu = new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("kande_paritolu")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>SooritaKanne_v4>kande_paritolu", Reader.LocalName);
            }
            if (Reader.LocalName=="kande_id") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKanne_v4>kande_id");
                }
                clazz.KandeId = new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("kande_id")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>SooritaKanne_v4>kande_id", Reader.LocalName);
            }
            if (Reader.LocalName=="ekande_liik") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKanne_v4>ekande_liik");
                }
                clazz.EkandeLiik = new global::Xtee.Core.Schema.XsdInt(Reader.ReadElementString("ekande_liik")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>SooritaKanne_v4>ekande_liik", Reader.LocalName);
            }
            if (Reader.LocalName=="on_paranduskanne") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKanne_v4>on_paranduskanne");
                }
                clazz.OnParanduskanne = new global::Xtee.Core.Schema.XsdBoolean(Reader.ReadElementString("on_paranduskanne")).Value;
            }
            if (Reader.LocalName=="eelmine_lahend_legacy") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKanne_v4>eelmine_lahend_legacy");
                }
                clazz.EelmineLahendLegacy = new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("eelmine_lahend_legacy")).Value;
            }
            if (Reader.LocalName=="eelmine_paevikukande_id") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKanne_v4>eelmine_paevikukande_id");
                }
                clazz.EelminePaevikukandeId = new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("eelmine_paevikukande_id")).Value;
            }
            if (Reader.LocalName=="on_kiirmenetlus") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKanne_v4>on_kiirmenetlus");
                }
                clazz.OnKiirmenetlus = new global::Xtee.Core.Schema.XsdBoolean(Reader.ReadElementString("on_kiirmenetlus")).Value;
            }
            if (Reader.LocalName=="kande_kuupaev") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKanne_v4>kande_kuupaev");
                }
                clazz.KandeKuupaev = new global::Xtee.Core.Schema.XsdDate(Reader.ReadElementString("kande_kuupaev")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>SooritaKanne_v4>kande_kuupaev", Reader.LocalName);
            }
            if (Reader.LocalName=="esitaja_nimi") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKanne_v4>esitaja_nimi");
                }
                clazz.EsitajaNimi = new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("esitaja_nimi")).Value;
            }
            if (Reader.LocalName=="esitaja_kood") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKanne_v4>esitaja_kood");
                }
                clazz.EsitajaKood = new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("esitaja_kood")).Value;
            }
            if (Reader.LocalName=="asutamisnumber") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKanne_v4>asutamisnumber");
                }
                clazz.Asutamisnumber = new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("asutamisnumber")).Value;
            }
            if (Reader.LocalName=="ametitoiming") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKanne_v4>ametitoiming");
                }
                global::Xtee.Core.SerializationMetadata metaAmetitoiming = this.GetMetaData<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.ISooritakanneAmetitoimingV4>(Reader, "sooritakanne_ametitoiming_v4", "http://producers.arireg.xtee.riik.ee/producer/arireg");
                clazz.Ametitoiming = metaAmetitoiming.NewEntity<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.ISooritakanneAmetitoimingV4>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("ametitoiming");
                    metaAmetitoiming.NewSerializer(clazz.Ametitoiming).ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            if (Reader.LocalName=="riigiloivud") {
                clazz.Riigiloivud = new System.Collections.Generic.List<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IRiigiloivTypeV4>();
                for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                            && (Reader.LocalName == "riigiloivud")); ) {
                    if ((Reader.IsElementNil() == false)) {
                        global::Xtee.Core.SerializationMetadata metaRiigiloivud = this.GetMetaData<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IRiigiloivTypeV4>(Reader, "riigiloivType_v4", "http://producers.arireg.xtee.riik.ee/producer/arireg");
                        global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IRiigiloivTypeV4 item = metaRiigiloivud.NewEntity<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IRiigiloivTypeV4>();
                        clazz.Riigiloivud.Add(item);
                        if ((Reader.IsEmptyElement == false)) {
                            Reader.ReadStartElement("riigiloivud");
                            metaRiigiloivud.NewSerializer(item).ReadXml(Reader);
                            Reader.ReadEndElement();
                        }
                        else {
                            Reader.Skip();
                        }
                    }
                    else {
                        clazz.Riigiloivud.Add(null);
                        Reader.Skip();
                    }
                }
            }
            if (Reader.LocalName=="markus") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKanne_v4>markus");
                }
                clazz.Markus = new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("markus")).Value;
            }
            if (Reader.LocalName=="registri_piirkond") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKanne_v4>registri_piirkond");
                }
                clazz.RegistriPiirkond = new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("registri_piirkond")).Value;
            }
            clazz.Ettevotjad = new System.Collections.Generic.List<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.ISooritakanneEttevotjadV4>();
            for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                        && (Reader.LocalName == "ettevotjad")); ) {
                if (Reader.LocalName=="ettevotjad") {
                    if (Reader.IsElementNil()) {
                        throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKanne_v4>ettevotjad");
                    }
                    global::Xtee.Core.SerializationMetadata metaEttevotjad = this.GetMetaData<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.ISooritakanneEttevotjadV4>(Reader, "sooritakanne_ettevotjad_v4", "http://producers.arireg.xtee.riik.ee/producer/arireg");
                    global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.ISooritakanneEttevotjadV4 item = metaEttevotjad.NewEntity<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.ISooritakanneEttevotjadV4>();
                    clazz.Ettevotjad.Add(item);
                    if ((Reader.IsEmptyElement == false)) {
                        Reader.ReadStartElement("ettevotjad");
                        metaEttevotjad.NewSerializer(item).ReadXml(Reader);
                        Reader.ReadEndElement();
                    }
                    else {
                        Reader.Skip();
                    }
                }
                else {
                    throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>SooritaKanne_v4>ettevotjad", Reader.LocalName);
                }
            }
            if ((clazz.Ettevotjad.Count < 1m)) {
                throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "ettevotjad");
            }
        }
    }
}
